package com.jcfk.eam.controller.equ;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.equ.EfaLiabilityExtentDTO;
import com.jcfk.eam.domain.dto.equ.EfaLiabilityExtentEquipDTO;
import com.jcfk.eam.domain.dto.equ.EfaLiabilityExtentPersonDTO;
import com.jcfk.eam.domain.query.equ.EfaLiabilityExtentQuery;
import com.jcfk.eam.service.equ.EfaLiabilityExtentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 设备责任范围
 *
 * @author yrl
 * @email 
 * @date 2025-04-22
 */
@Api(tags = {"设备责任范围管理"})
@RestController
@RequestMapping("efaliabilityextent")
public class EfaLiabilityExtentController {
    @Autowired
    private EfaLiabilityExtentService efaLiabilityExtentService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EfaLiabilityExtentQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询设备责任范围")
    @PostMapping("/queryByPage")
    public RetPage<EfaLiabilityExtentDTO> queryByPage(@RequestBody ReqPageParam<EfaLiabilityExtentQuery> requestParam) {
        // 设置默认排序
        if (requestParam.getSort().getOrderItem() == null) {
            ReqSortParam<EfaLiabilityExtentQuery> sortParam = new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<EfaLiabilityExtentDTO> page = new DataPage<EfaLiabilityExtentDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EfaLiabilityExtentDTO.class);
        page = efaLiabilityExtentService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EfaLiabilityExtentQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取设备责任范围列表", notes = "获取设备责任范围列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EfaLiabilityExtentEquipDTO>> getDetails(@RequestBody EfaLiabilityExtentQuery query) {

        List<EfaLiabilityExtentEquipDTO> list = efaLiabilityExtentService.getDetails(query);

        return RetResult.ok(list);
    }
    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EfaLiabilityExtentQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取设备责任范围列表", notes = "获取设备责任范围列表", httpMethod = "POST")
    @PostMapping("getPersons")
    public RetResult<List<EfaLiabilityExtentPersonDTO>> getPersons(@RequestBody EfaLiabilityExtentQuery query) {

        List<EfaLiabilityExtentPersonDTO> list = efaLiabilityExtentService.getPersons(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EfaLiabilityExtentQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取设备责任范围列表", notes = "获取设备责任范围列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EfaLiabilityExtentDTO>> getList(@RequestBody EfaLiabilityExtentQuery query) {

        List<EfaLiabilityExtentDTO> list = efaLiabilityExtentService.getList(query);

        return RetResult.ok(list);
    }


    /**
     *  设备责任范围
     * 根据id 查询详情
     */
    @ApiOperation("查询设备责任范围")
    @GetMapping("/get/{id}")
    public RetResult<EfaLiabilityExtentDTO> get(@PathVariable("id") String id) {
            EfaLiabilityExtentDTO item = efaLiabilityExtentService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理设备责任范围
     */
    @ApiOperation("保存设备责任范围")
    @PostMapping("/save")
    @RequestLogHeader(modular = "设备责任范围管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EfaLiabilityExtentDTO efaLiabilityExtent) {
        // 数据校验
        RetResult ret = efaLiabilityExtentService.isValid(efaLiabilityExtent);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(efaLiabilityExtent.getId())){
            // 新增
                efaLiabilityExtentService.insert(efaLiabilityExtent);
        } else{
            // 更新
                efaLiabilityExtentService.update(efaLiabilityExtent);
        }
        return RetResult.ok();

    }


    /**
     *  批量删除设备责任范围
     */
    @ApiOperation("批量删除设备责任范围")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "设备责任范围管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EfaLiabilityExtentDTO efaLiabilityExtent) {
        return RetResult.ok(efaLiabilityExtentService.logicDeleteList(efaLiabilityExtent));
    }


    /**
     *  删除设备责任范围
     */
    @ApiOperation("删除设备责任范围")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "设备责任范围管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        efaLiabilityExtentService.deleteItem(id);
        return RetResult.ok();
    }
}
